package com.globallogic.oopbasics.competence.io;

import java.io.FileNotFoundException;
import java.util.Scanner;

import com.globallogic.oopbasics.competence.container.TotalCompetence;
import com.globallogic.oopbasics.competence.entity.PersonalCompetency;
import com.globallogic.oopbasics.competence.entity.TechCompetency;
import com.globallogic.oopbasics.competence.exception.LevelException;

public class CompetencyScanner extends ReadWithScanner {

	private TotalCompetence competenciesList;

	public TotalCompetence getCompetenciesList() {
		return competenciesList;
	}

	public CompetencyScanner(String aFileName) throws FileNotFoundException,
			IllegalArgumentException, LevelException {
		super(aFileName);
	}

	@Override
	protected void init() {
		competenciesList = new TotalCompetence();
	}

	/**
	 * Overridable method for processing lines in different ways.
	 * @throws LevelException 
	 */
	@Override
	protected void processLine(String aLine) throws LevelException {
		// use a second Scanner to parse the content of each line
		Scanner scanner = new Scanner(aLine);
		scanner.useDelimiter("~");
		if (scanner.hasNext()) {

			int id = scanner.nextInt();
			String name = scanner.next();
			String type = scanner.next();
			double value = scanner.nextDouble();

			if (type.equals("tech")) {
				competenciesList.add(new TechCompetency(id, name, value));
			} else {
				competenciesList.add(new PersonalCompetency(id, name, value));
			}

		} else {
			throw new IllegalArgumentException(
					"Empty or invalid line. Unable to process.");
		}
	}
}
